<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of RssController
 *
 * @author tuancasi
 */
class RssController extends Controller {

    //put your code here
    public function actionIndex() {

	$this->render('index');
    }

    /*
     * RENDER FEED
     */

    public function actionGet($name) {

	Yii::import('common.extensions.feed.*');
	switch ($name) {
	    case 'equipment':
		$postType = Ad::TYPE_EQUIPMENT;
		break;
	    case 'supply':
		$postType = Ad::TYPE_SUPPLY;
		break;
	    case 'vendor':
		$postType = Ad::TYPE_VENDOR;
		break;
	    case 'technical':
		$postType = Ad::TYPE_TECHNICAL;
		break;
	    case 'resume':
		$postType = Ad::TYPE_RESUME;
		break;
	    case 'job':
		$postType = Ad::TYPE_JOB;
		break;
	    case 'advertise': default :
		$postType = Ad::TYPE_ADVERTISE;
		break;
	}
	$model = Ad::model()->with(array('categories', 'mainCategory'))->findAll(array(
	    'condition' => 'type=' . $postType,
	    'limit' => 10
	));
	// specify feed type
	Yii::import('ext.feed.*');
// specify feed type
	$feed = new EFeed(EFeed::RSS1);
	$feed->title = 'Get the RSS of ' . $name;
	//$feed->link = SITE_NAME_URL . 'rss/get/' . $name;
	//$feed->link = 'http://www.yiiframework.com/extension/efeed/';
	//$feed->description = '';
	//$feed->RSS1ChannelAbout = 'http://www.ramirezcobos.com/about';
	//
// create our item
	if ($model) {

	    foreach ($model as $itemModel) {
		$sCat = '';
		$sCat = $itemModel->mainCategory->name;
		if ($itemModel->categories) {
		    foreach ($itemModel->categories as $cat) {
			$sCat.= ',' . $cat->name;
		    }
		}
		$item = $feed->createNewItem();
		$item->title = $item->title;
//		$item->link = 'http://www.yiiframework.com/ad/' . $itemModel->ad_id;
		$item->link = SITE_NAME_URL . 'ad/' . $itemModel->ad_id;
		$item->date = $itemModel->updated;
		$item->description = 'Category: ' . $sCat . '<br> Content: ' . $itemModel->content;
		//$item->addTag('dc:subject', 'Subject Testing');
		// add it to the feed
		$feed->addItem($item);
	    }
	} else {
	    $item = $feed->createNewItem();
	    $item->title = 'Warning!';
	    $item->link = SITE_NAME_URL . 'rss/get/' . $name;
	    $item->date = time();
	    $item->description = 'There is no post to get';
	    //$item->addTag('dc:subject', 'Subject Testing');
	    // add it to the feed
	    $feed->addItem($item);
	}
	$feed->generateFeed();
    }

}

